(function($) {
    // jQuery plugin definition
    $.fn.reverseText = function(params) {
        // merge default and user parameters
        params = $.extend( {minlength: 0, maxlength: 99999}, params);
        // traverse all nodes
        this.each(function() {
            // express a single node as a jQuery object
            var $t = $(this);
            // find text
            var origText = $t.text(), newText = '';
            // text length within defined limits?
            if (origText.length >= params.minlength &&  origText.length <= params.maxlength) {
                // reverse text
                for (var i = origText.length-1; i >= 0; i--) newText += origText.substr(i, 1);
                $t.text(newText);
            }
        });
        // allow jQuery chaining
        return this;
    };

    /*PLUGIN DEFINITION*/
    $.fn.checkAnswers = function(params) {       
        /*MARGE DEFAULT AND USER PARAMETERS*/
        params = $.extend( {
            limit: false, 
            cnt: 0 , 
            values_to_check: new Array('','Ostavi prazno da bi obrisao odgovor')
        }, params );
        /*GO TROUG EACH ELEMENTS AND COMAPRE VLUES FROM PPARAMS ARRAY*/
        this.each(function() {
            var $t = $(this);
            /*COMPARE INPUT VALUES WITH ARRAY VALUES*/
            if(($t.val()!=params.values_to_check[0])&&($t.val()!=params.values_to_check[1])){
                params.cnt++;
                if(params.cnt==2){
                    params.limit = true; 
                    return;   
                }
            }
        })
        return params.limit;
    };

    /*PLUGIN DEFINITION*/
    $.fn.checkQuestion = function(params) {       
        /*MARGE DEFAULT AND USER PARAMETERS*/
        params = $.extend( {
            length_limit: 0, 
            message: 'Molimo unestite pitanje.'
        }, params );
        var $t = $(this);
        /*CHECK USER INPUT CHARACTER LENGTH*/
        if (!$t.val().length == params.length_limit) return true; else return false;
    };

    /*PLUGIN DEFINITION*/
    $.fn.checkOption = function(params) {       
        /*MARGE DEFAULT AND USER PARAMETERS*/
        params = $.extend( {
            custom_style: false, 
            con: false, 
            message: 'Molimo odaberite stil.'
        }, params );
        var $t = $(this);
        /*CHECK DROPDOWN FOR CUSTOM STYLE IF RADIO IS SELECTED*/
        $("input[name='stylecustom']:checked").each(function(){
            if ($(this).attr('checked')){
                params.con = true; 
            }
        });
        if(params.con) {
            if ($t.val()== 0){
                return false
            }else return true
        }else return true 
    };
    /*PLUGIN DEFINITION*/
    $.fn.checkPollWidth = function() {       
        var custom_checked = false;
        /*CHECK DROPDOWN FOR CUSTOM STYLE IF RADIO IS SELECTED*/
        $("input[name='stylecustom']:checked").each(function(){
            if ($(this).attr('checked')){
                custom_checked = true; 
            }
        });
        if(custom_checked) return true;
        var con_poll_width = false;
        /*CHECK WIDGET SIZT RADIO IS SELECTED*/
        $("input[name='poll_width']:checked").each(function(){
            if ($(this).attr('checked')){
                con_poll_width = true; 
            }
        });        
        if(con_poll_width){
            return true;
        }else return false;
    };
})(jQuery);
